package com.qikux.controller;

import com.qikux.annotation.*;
import com.qikux.annotition.Authentication;
import com.qikux.consts.AppConst;
import com.qikux.entity.User;
import com.qikux.service.UserService;
import com.qikux.utils.ContextUtils;
import com.qikux.vo.ResultVo;

@Component
@RequestMapping("/change")
public class ChangeController {

    @Resource
    private UserService userService;

    @Authentication
    @RequestMapping(value = "/nickName",method = RequestMethod.GET)
    @ResponseBody
    public ResultVo<String> nickName(){
        String nickName = ContextUtils.getRequest().getParameter("nickName");
        User user = (User) ContextUtils.getRequest().getSession().getAttribute(AppConst.LOGIN_FLAG);
        user.getInfo().setNickName(nickName);
        return userService.updateNickNameByUserId(nickName,user.getId());
    }

    @Authentication
    @RequestMapping(value = "/sex",method = RequestMethod.GET)
    @ResponseBody
    public ResultVo<String> sex(){
        String sex = ContextUtils.getRequest().getParameter("sex");
        User user = (User) ContextUtils.getRequest().getSession().getAttribute(AppConst.LOGIN_FLAG);
        user.getInfo().setSex(sex);
        return userService.updateSexByUserId(sex,user.getId());
    }

    @Authentication
    @RequestMapping(value = "/email",method = RequestMethod.GET)
    @ResponseBody
    public ResultVo<String> email(){
        String oldEmail = ContextUtils.getRequest().getParameter("oldEmail");
        String newEmail = ContextUtils.getRequest().getParameter("newEmail");
        User user = (User) ContextUtils.getSession().getAttribute(AppConst.LOGIN_FLAG);
        ResultVo<String> stringResultVo = userService.updateEmial(user, oldEmail, newEmail);
        if (stringResultVo.isStatus())  user.setEmail(newEmail);
        return stringResultVo;
    }
}
